﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;
using System.Text.RegularExpressions;
using InHD.Properties;
using InHD.dataaccess;

namespace InHD.model
{
    class CompanyInfo : IDataErrorInfo
    {
        #region Creation

        public static CompanyInfo CreateNewCompanyInfo()
        {
            return new CompanyInfo();
        }

        public static CompanyInfo CreateCompanyInfo(
            string taxcode,
            string name,
            string address,
            string director,
            string email,
            string phone)
        {
            return new CompanyInfo
            {
                Taxcode = taxcode,
                Name = name,
                Address = address,
                Director = director,
                Email = email,
                Phone = phone
            };
        }

        protected CompanyInfo()
        {
        }

        #endregion // Creation

        #region State Properties

        /// <summary>
        /// Gets/sets the e-mail address for the CompanyInfo.
        /// </summary>
        public string Email { get; set; }

        /// <summary>
        /// Gets/sets the CompanyInfo's name.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets/sets the CompanyInfo's address.
        /// </summary>
        public string Address { get; set; }

        /// <summary>
        /// Gets/sets the CompanyInfo's phone.
        /// </summary>
        public string Phone { get; set; }

        /// <summary>
        /// Gets/sets the CompanyInfo's director.
        /// </summary>
        public string Director { get; set; }

        /// <summary>
        /// Gets/sets the CompanyInfo's tax code.
        /// </summary>
        public string Taxcode { get; set; }

        #endregion // State Properties

        #region IDataErrorInfo Members

        string IDataErrorInfo.Error { get { return null; } }

        string IDataErrorInfo.this[string propertyName]
        {
            get { return this.GetValidationError(propertyName); }
        }

        #endregion // IDataErrorInfo Members

        #region Validation

        /// <summary>
        /// Returns true if this object has no validation errors.
        /// </summary>
        public bool IsValid
        {
            get
            {
                foreach (string property in ValidatedProperties)
                    if (GetValidationError(property) != null)
                        return false;

                return true;
            }
        }

        static readonly string[] ValidatedProperties = 
        { 
            "Email", 
            "Name", 
            "Director",
            "Phone",
            "Taxcode"
        };

        string GetValidationError(string propertyName)
        {
            if (Array.IndexOf(ValidatedProperties, propertyName) < 0)
                return null;

            string error = null;

            switch (propertyName)
            {
                case "Email":
                    error = this.ValidateEmail();
                    break;

                case "Name":
                    error = this.ValidateName();
                    break;

                case "Director":
                    error = this.ValidateDirector();
                    break;

                case "Phone":
                    error = this.ValidatePhone();
                    break;

                case "Taxcode":
                    error = this.ValidateTaxcode();
                    break;

                default:
                    Debug.Fail("Unexpected property being validated on CompanyInfo: " + propertyName);
                    break;
            }

            return error;
        }

        private string ValidatePhone()
        {
            if (utilities.ValidatingUtilities.ValidateMissing(this.Phone))
            {
                return Resources.InHD_Error_MissingField;
            }
            else
            {
                if (utilities.ValidatingUtilities.ValidateNumber(this.Phone))
                {
                    return Resources.InHD_RequiredNumber;
                }
            }

            return null;
        }

        private string ValidateTaxcode()
        {
            if (utilities.ValidatingUtilities.ValidateMissing(this.Taxcode))
            {
                return Resources.InHD_Error_MissingField;
            }
            else
            {
                if (utilities.ValidatingUtilities.ValidateNumber(this.Taxcode))
                {
                    return Resources.InHD_RequiredNumber;
                }
            }

            return null;
        }

        string ValidateEmail()
        {
            if (utilities.ValidatingUtilities.ValidateMissing(this.Email))
            {
                return Resources.InHD_Error_MissingField;
            }
            else
            {
                if (utilities.ValidatingUtilities.ValidateEmail(this.Email))
                {
                    return Resources.InHD_InvalidEmail;
                }
            }

            return null;
        }

        string ValidateName()
        {
            if (utilities.ValidatingUtilities.ValidateMissing(this.Name))
            {
                return Resources.InHD_Error_MissingField;
            }

            return null;
        }

        string ValidateDirector()
        {
            if (utilities.ValidatingUtilities.ValidateMissing(this.Director))
            {
                return Resources.InHD_Error_MissingField;
            }
           
            return null;
        }

        
        #endregion // Validation

        public CompanyInfo CreateShallowCopy()
        {
            return (CompanyInfo)this.MemberwiseClone();
        }
    }
}
